package cn.changgou.service.impl;

import cn.changgou.dao.BrandMapper;
import cn.changgou.goods.pojo.Brand;
import cn.changgou.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /*根据分类ID查询品牌集合-分类ID*/
    @Override
    public List<Brand> findByCategory(Integer categoryId) {
        return brandMapper.findByCategory(categoryId);
    }

    /*分页+条件搜索*/
    @Override
    public PageInfo<Brand> findPage(Brand brand, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Example example = createExample(brand);
        List<Brand> brands = brandMapper.selectByExample(example);
        return new PageInfo<Brand>(brands);
    }

    /*分页搜索*/
    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {
        //PageHelper.startPage(page, size)分页实现，后面的查询紧跟集合查询
        PageHelper.startPage(page, size);
        List<Brand> brands = brandMapper.selectAll();
        return new PageInfo<Brand>(brands);
    }

    /*根据品牌信息多条件搜索*/
    @Override
    public List<Brand> findList(Brand brand) {
        Example example = createExample(brand);
        return brandMapper.selectByExample(example);
    }

    /*根据ID删除品牌*/
    @Override
    public void delete(Integer id) {
        //删除品牌->Mapper.deleteByPrimaryKey(Object)
        brandMapper.deleteByPrimaryKey(id);
    }

    /*根据ID修改品牌数据*/
    @Override
    public void update(Brand brand) {
        //修改品牌->Mapper.updateByPrimaryKeySelective(T)
        brandMapper.updateByPrimaryKeySelective(brand);
    }

    /*增加品牌*/
    @Override
    public void add(Brand brand) {
        //增加品牌->Mapper.insertSelective(T)
        //带有Selective的方法会忽略空值
        brandMapper.insertSelective(brand);
    }

    /*根据ID查询*/
    @Override
    public Brand findById(Integer id) {
        //根据ID查询->Mapper.selectByPrimaryKey(Object) 根据主键查询
        return brandMapper.selectByPrimaryKey(id);
    }

    /*查询所有*/
    @Override
    public List<Brand> findAll() {
        //查询所有->通用Mapper.selectAll()
        return brandMapper.selectAll();
    }

    /*条件构建*/
    public Example createExample(Brand brand){
        //自定义条件搜索对象 Example
        Example example = new Example(Brand.class);
        //条件构造器
        Example.Criteria criteria = example.createCriteria();
        if (brand !=null){
            //brand.name!=null根据名字模糊搜索
            if (!StringUtils.isEmpty(brand.getName())){
                /*
                 * 1.Brand的属性名
                 * 2.占位符参数，搜索的条件
                 * */
                criteria.andLike("name", "%" + brand.getName() + "%");
            }
            //brand.letter!=null根据首字母搜索
            if (!StringUtils.isEmpty(brand.getLetter())){
                criteria.andEqualTo("letter", brand.getLetter());
            }
        }
        return example;
    }
}
